<template>
  <view class="container">
    <form>
      <view class="form-item">
        <label>原料类型</label>
        <uni-data-picker :localdata="typeList" v-model="form.typeId" popup-title="请选择原料类型"  @nodeclick="onnodeclick"></uni-data-picker>
      </view>
      <view class="form-item">
        <label>原料名称</label>
       <uni-easyinput v-model="form.materialName" placeholder="请输入原料名称" />
      </view>
      <view class="form-item">
        <label>批次号</label>
        <uni-easyinput v-model="form.lotNumber" placeholder="请输入物料批次号" />
      </view>
      <view class="form-item">
        <label>总检验数量</label>
        <uni-easyinput v-model="form.quantity" placeholder="请输入总检验数量" />
      </view>
      <view class="form-item">
        <label>单位</label>
		<uni-data-select  
			v-model="form.unitId"
		  :localdata="unitList"
		  @change="change">
		  </uni-data-select>
      </view>
      <view class="form-item">
        <label>合格数量</label>
        <uni-easyinput v-model="form.passNum" placeholder="请输入合格数量" />
      </view>
      <view class="form-item">
        <label>方案合格率</label>
       <uni-easyinput v-model="form.passRate" placeholder="请输入方案合格率" />
      </view>
      <view class="form-item">
        <label>质检合格率</label>
       <uni-easyinput v-model="form.trueRate" placeholder="请输入质检合格率" disabled/>
      </view>
      <view class="form-item">
        <label>检验结果</label>
        <uni-data-select
        	v-model="form.result"
          :localdata="result" disabled>
          </uni-data-select>
      </view>
	  <view class="form-item">
	    <label>质检员</label>
	    <uni-data-select
	    	v-model="form.workerId"
	      :localdata="workerList">
	      </uni-data-select>
	  </view>
      <view class="form-item">
        <label>缺陷描述</label>
		<uni-easyinput type="textarea" autoHeight v-model="form.defectsDescription" placeholder="请输入缺陷描述"></uni-easyinput>
      </view>
      <view class="form-item">
        <label>备注</label>
		<uni-easyinput type="textarea" autoHeight v-model="form.notes" placeholder="请输入备注"></uni-easyinput>
      </view>
      
      <button formType="submit" class="submit-btn" @click="submitForm()">提交</button>
    </form>
  </view>
</template>

<script>
export default {
	data() {
		return {
			workerList: [],
			id: undefined,
			//单位列表
			unitList: [],
			//物料类型
			typeList:[],
			result: [
				{ value: 0, text: "合格" },
				{ value: 1, text: "不合格" },
				{ value: 2, text: "待确认" },
				{ value: 3, text: "待质检" }
			],
		  form:{
			  id: undefined,//明细id
			  typeId: undefined,//原料类型id
			  materialName: undefined,//原料名称
			  qrCode: undefined,//二维码
			  lotNumber: undefined,//批次号
			  quantity: undefined,//总检验数量
			  unitId: undefined,//单位id
			  inspectionDate: undefined,//检验日期
			  workerId:undefined,//检验人员id
			  defectsDescription: undefined,//缺陷描述
			  passRate: undefined,//方案合格率
			  trueRate:undefined,//实际合格率
			  passNum: undefined,//最终合格数量
			  createdBy: undefined,//创建人id
			  createdAt: undefined,//创建时间
			  updatedAt: undefined,//更新时间
			  notes: undefined,//备注
			  schemeId: undefined,//方案id
			  materialId:undefined,//原料id
			},
    };
  },
  onLoad(options) {
      // 从 options 对象中获取传递过来的 id 参数
      this.id = options.id;
	 this.getDetails();
    },
  created() {
	this.getTypeList();
  	this.getUnitList();
	this.getWorkerList();
  },
  methods: {
	  getTypeList() {
	    uni.request({
	      url: 'http://127.0.0.1:1002/mesMaterial/deptTree',
	      method: 'GET',
	      success: (res) => {
	        if (res.data.code === 200 && res.data.data) {
	          const newData = [];
	          res.data.data.forEach(item => {
	            const newItem = {
	              text: item.label,
	              value: item.id,
	              children: []
	            };
	            if (item.children && item.children.length > 0) {
	              newItem.children = this.processChildren(item.children);
	            }
	            newData.push(newItem);
	          });
	          this.typeList = newData;
	        }
	      },
	      fail: (err) => {
	        console.error('请求失败:', err);
	      }
	    });
	  },
	  getWorkerList(){
		  uni.request({
		      url: 'http://127.0.0.1:1005/mesMaterialWorker/getList',
		      method: 'GET',
		      // 这里添加逗号，分隔 data 和 success 属性
		      success: (res) => {
		          if (res.data.code === 200) {
		  			this.workerList = res.data.data.map((item, index) => ({
					  value: item.id,
					  text: item.nickName
					}));
		          } else {
		              console.log(res.data.msg);
		          }
		      },
		      // 添加 fail 回调函数，处理请求失败的情况
		      fail: (err) => {
		          console.error('请求失败:', err);
		      }
		  });
	  },
	  getDetails() {
	  		/* const id = this.id; */
	  	     uni.request({
	  	         url: 'http://127.0.0.1:1005/mesMaterialQualityDetail/getById1',
	  	         method: 'GET',
				 data:{
					 id : this.id
				 },
	  	         // 这里添加逗号，分隔 data 和 success 属性
	  	         success: (res) => {
	  	             if (res.data.code === 200) {
						 console.log(res.data.data);
						 this.form = res.data.data;
						/* this.form.typeId = parseInt(this.form.typeId); */
	  	             } else {
	  	                 console.log(res.data.msg);
	  	             }
	  	         },
	  	         // 添加 fail 回调函数，处理请求失败的情况
	  	         fail: (err) => {
	  	             console.error('请求失败:', err);
	  	         }
	  	     });
	  	 },
	  onnodeclick(node) {
		  this.form.typeId = node.value;
	  },
	 processChildren(children) {
	   const result = [];
	   children.forEach(child => {
	     const newChild = {
	       text: child.label,
	       value: child.id,
	       children: []
	     };
	     if (child.children && child.children.length > 0) {
	       newChild.children = this.processChildren(child.children);
	     }
	     result.push(newChild);
	   });
	   return result;
	 },
    submitForm() {
		uni.request({
			 url: 'http://127.0.0.1:1005/mesMaterialQualityDetail/update',
			 method: 'POST',
			 header: {
			   'Content-Type': 'application/json'
			 },
			 data:this.form,
			 // 这里添加逗号，分隔 data 和 success 属性
			 success: (res) => {
				 if (res.data.code === 200) {
					 uni.showToast({
					 	title:'提交成功',
						icon:'success'
					 });
					 uni.navigateBack({delta: 1});
				 }
			 },
			 // 添加 fail 回调函数，处理请求失败的情况
			 fail: (err) => {
				 uni.showToast({
				 	title:'提交失败',
				 	icon:'error'
				 });
			 }
		 });
    },
	getUnitList(){
	   uni.request({
			 url: 'http://127.0.0.1:1005/mesMaterialUnit/unitList',
			 method: 'GET',
			 // 这里添加逗号，分隔 data 和 success 属性
			 success: (res) => {
				 if (res.data.code === 200) {
					this.unitList = res.data.data.map((item, index) => ({
					  value: item.id,
					  text: item.unitName
					}));
				 }
			 },
			 // 添加 fail 回调函数，处理请求失败的情况
			 fail: (err) => {
				 console.error('请求失败:', err);
			 }
		 });
	},
	
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
  background-color: #fff;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.title {
  font-size: 20px;
}

.form-item {
  margin-bottom: 15px;
}

.form-item label {
  display: block;
  margin-bottom: 5px;
}

.form-item input,
.form-item textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

.picker-value {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

.submit-btn {
  background-color: #008CF8;
  color: white;
  height: 40px;
  width: 60%;
  border: none;
  border-radius: 10px;
  line-height: 40px;
  margin: 20px auto 0;
}


.form-item input,
.picker-value {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  height: 40px; /* 统一高度 */
  line-height: 20px; /* 统一行高，可根据实际微调 */
  font-size: 14px; /* 统一字体大小 */
  color: #333; /* 统一字体颜色 */
  background-color: #fff; /* 统一背景色 */
}
.form-item input:focus {
  border-color: #87CEEB;
  outline: none;
}

</style>